package com.extra_archive.framework.utils {

	//==================================================================
	//@class name  :  ArrayUtils.as
	//@author          : vkimone. KimKiJeung  (http://kimkijeung.com)
	//@last update   : 2007. 11. 19
	//@version         : V1.0
	//==================================================================
	public class ArrayUtils {

		public static var LEFT : String = "left";
		public static var RIGHT : String = "right";	

		
		public static function findElement(array : Array,element : *) : int {
			for(var i : uint = 0 ;i <= array.length;i++) {
				//trace(array[i]);
				if(array[i] == element) {
					return i;
				}
			}
			return -1;
		};

		public static function removeElement( inList : Array, inValue : Object ) : Boolean {

			for (var i : Number = 0;i < inList.length;++i) {

				if (inList[i] == inValue) {

					inList.splice(i, 1);
					return true;
					break;
				}
			}
			return false;
		}

		
		public static function insertElementAt( inList : Array, inPosition : int, inValue : Object ) : Boolean {

			// valid position?
			if (inPosition > inList.length || inPosition < 0) {

				return false;
			} else {

				if (inPosition == inList.length) {

					// position is same as length, push it
					inList.push(inValue);
				} else if (inPosition == 0) {

					if (inList.length == 0) {

						// add at start
						inList.push(inValue);
					} else {

						// add at end
						inList.unshift(inValue);
					}
				} else {

					// add at position specified
					inList.splice(inPosition, 0, inValue);
				}

				return true;
			}
		}

		
		public static function findElementObject(array : Array,objProperty : *,element : *) : int {
			for(var i : uint = 0 ;i <= array.length;i++) {
				for(var j:* in array[i]) {
					if(j == objProperty && array[i][j].toLowerCase() == element.toLowerCase()) {
						return i;
					}
				}
			}
			return -1;
		}

		public static function findObjectByProperty(array : Array,objProperty : *,element : *) : Object {
			for(var i : uint = 0 ;i <= array.length;i++) {
				if(array[i]) {
					if(array[i][objProperty] == element) {
						return array[i];
					}
				}
			}
			return null;
		}

		
		
		
		public static function getRandomElement(array : Array) : void {
   			 //return array[Math.random()*(array.length)];
		}

		public static function deleteElement(array : Array,index : int) : void {
			array.splice(index, 1);   	
		}

		
		public static function shuffleArray(array : Array) : Array {
			var temp_arr : Array = array.concat();
			var new_arr : Array = new Array();
      
			while(temp_arr.length) {
      	//new_arr.push(temp_arr.splice(random(temp_arr.length),1)[0]);
			}
      
			return new_arr;
		}

		
		public static function getShiftArr(array : Array,direction : String) : Array {
			var temp_arr : Array = array.concat();
			if(direction == RIGHT) {
				temp_arr.unshift(temp_arr.pop());
			}else if(direction == LEFT) {
				temp_arr.push(temp_arr.shift());
			} else {
				trace("Parameter Error-----> direction");
			}
			return temp_arr;
		}

		
		
		
		public static function addValueToLimited(arr : Array,limitNum : int, value : Number) : Array {
			arr.push(value);
			if(arr.length > limitNum) {
				arr.shift();
			}
			return arr;
		}

		
		public static function averageValue(arr : Array) : Number {	
			var len : int = arr.length;
			var count : Number = 0;	
			for(var i : int = 0;i < len;i++) {	
				count += (arr[i]);
			}
			return (count / len);
		}

		public static function averageValueExceptionZero(arr : Array) : Number {	
			var len : int = arr.length;
			var total : Number = 0;
			var count : Number = 0;	
			for(var i : int = 0;i < len;i++) {
				//zero
				var value:Number=arr[i];
				if(value) {
					total++;
					count += value;
				}
			}
			if(total){
				return (count / total);
			}else{
				return 0;
			}
			
		}
	}
}
